package net.util;

import java.util.ArrayList;

public class ArrayList2<E> extends ArrayList<E>
{
	private static final long serialVersionUID = -318090484497588568L;

	private final boolean set;
	
	public ArrayList2()
	{
		super();
		set = false;
	}
	
	public ArrayList2(boolean setBehavior)
	{
		super();
		set = setBehavior;
	}
	
	/**
	 * Returns the element at index <code>e</code> measured from the back of the list.
	 * */
	public E getReversed(int e)
	{
		return get(size()-(e+1));
	}
	
	/**
	 * Returns, but does not remove the first element in the list. Queue-like behavior.
	 * */
	public E peekF()
	{
		return get(0);
	}
	
	/**
	 * Returns, but does not remove the last element in the list. Stack-like behavior.
	 * */
	public E peekL()
	{
		return get(size()-1);
	}
	
	/**
	 * Returns and removes the first element in the list. Queue-like behavior.
	 * */
	public E popF()
	{
		return remove(0);
	}
	
	/**
	 * Returns and removes the last element in the list. Stac-like behavior.
	 * */
	public E popL()
	{
		return remove(size()-1);
	}

	public double sum(SumCalc<E> calc)
	{
		double d = 0;
		
		for(E e : this)
		{
			d += calc.evaluate(e);
		}
		
		return d;
	}

	public boolean add(E e)
	{
		if (set && contains(e))
		{
			return false;
		}
		
		return super.add(e);
	}
	
	public void add(int i, E e)
	{
		if (set && contains(e))
		{
			return;
		}
		
		super.add(i, e);
	}
}
